//
//  FirListController.m
//  OCDemo
//
//  Created by BH on 15/9/24.
//  Copyright © 2015年 baohan. All rights reserved.
//

#import "FirListController.h"
#import "UIImage+UIColor.h"
#import "Masonry.h"
#import "GlobalDefine.h"

@interface FirListController ()<UITableViewDataSource,UITableViewDelegate>
{
    CGFloat headImgH;
    MASConstraint *headerImgLeftConstraint;//左边约束
    MASConstraint *headerImgWidthConstraint;//宽的约束
    MASConstraint *headerImgTopConstraint;//上边的约束
    MASConstraint *headerImgHeightConstraint;//高的约束
}
@property (strong, nonatomic) UIImageView *headImg;

@property (weak, nonatomic) IBOutlet UITableView *table;

@end

@implementation FirListController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    headImgH = 180;
    
    [self setNavigationBar];
    
    [self setHeaderView];
}

-(void)setNavigationBar
{
    UIButton *setBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [setBtn setTitle:@"设置" forState:UIControlStateNormal];
    [setBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [setBtn addTarget:self action:@selector(settingAction:) forControlEvents:UIControlEventTouchUpInside];
    setBtn.frame = CGRectMake(0, 0, 50, 30);
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:setBtn];
    
    self.navigationItem.leftBarButtonItem = leftItem;
}

-(void)setHeaderView
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[[UIColor clearColor]colorWithAlphaComponent:0]] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    _table.contentInset = UIEdgeInsetsMake(headImgH, 0, 0, 0);
    
    _headImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"header.jpg"]];
    _headImg.backgroundColor = [UIColor clearColor];
    [_table addSubview:_headImg];
    
    [_headImg mas_makeConstraints:^(MASConstraintMaker *make) {
        headerImgLeftConstraint = make.left.equalTo(@0);
        headerImgTopConstraint = make.top.equalTo(@(-headImgH));
        headerImgWidthConstraint = make.width.equalTo(@(_table.frame.size.width));
        headerImgHeightConstraint = make.height.equalTo(@(headImgH));
    }];
}

-(void)settingAction:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - scrollview delegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetY = scrollView.contentOffset.y;
    CGFloat offsetX = (offsetY+headImgH)/2;
    
    if (offsetY < -headImgH) {
        headerImgLeftConstraint.offset = offsetX;
        headerImgTopConstraint.offset = offsetY;
        headerImgWidthConstraint.offset = _table.frame.size.width + fabs(offsetX) * 2;
        headerImgHeightConstraint.offset = -offsetY;
    }
    
    CGFloat alpha = (offsetY+headImgH)/headImgH;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[ColorRGB(94, 158, 236) colorWithAlphaComponent:alpha]] forBarMetrics:UIBarMetricsDefault];

    alpha = fabs(alpha);
    alpha = fabs(1-alpha);
    alpha = alpha<0.5?0.5:alpha;
    _headImg.alpha = alpha;
}

#pragma mark - tableView datasource & delegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identify = @"FirListCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
    }
    
    cell.textLabel.text = [NSString stringWithFormat:@"index %ld",(long)indexPath.row];
    
    return cell;
}

@end
